package com.cms.admin.action.common;

import com.cms.admin.action.BaseAction;
import com.cms.admin.model.DepartmentList;
import com.cms.admin.model.DepartmentListExt;
import com.cms.admin.model.UserList;
import com.cms.admin.service.common.DepartmentService;
import com.cms.admin.service.common.TemplateService;
import com.cms.admin.util.Constants;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by YYY-Work on 2017/4/27.
 */
public class DepartmentAction extends BaseAction implements ModelDriven{


    private DepartmentService departmentListService;
    private TemplateService templateService;
    private DepartmentListExt search=new DepartmentListExt();
    private DepartmentList departmentList;

    public String ajaxQueryDepartmentList()
    {
        LOG.info("Start ArticleAction process,method is ajaxQuery");
        List<DepartmentList> departmentList=departmentListService.queryDepartmentList(search);
        String res = null;
        try {

            res=getQueryResult(departmentList);
            JSONObject o = new JSONObject();
            o.put("content", res);
            return ajaxJsonSuccessMessage(o);
        }catch (Exception e)
        {
            LOG.error("ajaxQueryDepartmentList",e);
            return ajaxJsonErrorMessage(Constants.INFO_SYS_ERROR);
        }

    }

    public String ajaxAddDepartmentList()
    {
        request= ServletActionContext.getRequest();
        initBathPath();
        List<DepartmentList> departmentLists=departmentListService.getDepartmentListByName(search);
        if (null==departmentLists||departmentLists.size()==0)
        {
            search.setCreateDate(new Date());
            search.setUpdateDate(new Date());
            search.setUpdateUserid(((UserList)request.getSession().getAttribute(Constants.USER_OBJECT)).getUserid());
            departmentListService.saveDepartmentList(search);
        }else
        {
            return null;
        }
        return "success";
    }

    public String ajaxModDepartmentList()
    {
        request=ServletActionContext.getRequest();
        initBathPath();
        search.setUpdateDate(new Date());
        search.setUpdateUserid(((UserList)request.getSession().getAttribute(Constants.USER_OBJECT)).getUserid());
        departmentListService.updateDepartmentList(search);
        return "success";
    }

    public String requestDepartmrntList()
    {
        initBathPath();
        return "success";
    }

    public String requestAddDepartmrnt()
    {
        initBathPath();
        return "success";
    }

    public String requestModDepartmrnt()
    {
        initBathPath();
        List<DepartmentList> departmentLists=departmentListService.getDepartmentListById(search);
        if (null!=departmentLists&&departmentLists.size()>0)
        {
            departmentList=departmentLists.get(0);
            return "success";
        }else
        {
            return null;
        }

    }

    public DepartmentService getDepartmentListService() {
        return departmentListService;
    }

    public void setDepartmentListService(DepartmentService departmentListService) {
        this.departmentListService = departmentListService;
    }

    public TemplateService getTemplateService() {
        return templateService;
    }

    public void setTemplateService(TemplateService templateService) {
        this.templateService = templateService;
    }

    @Override
    public Object getModel() {
        return search;
    }

    private String getQueryResult(List<DepartmentList> departmentList)
            throws Exception {
        initBathPath();
        HashMap map=new HashMap();
        map.put("departmentList",departmentList);
        map.put("basePath",getBasePath());
        String result = "";
        result = templateService.generateContentByFreeMarker(
                Constants.TEMPLATE_DEP_QUERY_RESULT, map);
        return result;
    }

    public DepartmentList getDepartmentList() {
        return departmentList;
    }

    public void setDepartmentList(DepartmentList departmentList) {
        this.departmentList = departmentList;
    }
}
